package com.cloud.config;

import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Flux;

import java.util.Arrays;
import java.util.List;

@Configuration
public class HelloConfig {

    @Bean
    HelloServiceInstanceListSupplier helloServiceInstanceListSupplier() {
        return new HelloServiceInstanceListSupplier("say-hello");
    }

    static class HelloServiceInstanceListSupplier implements ServiceInstanceListSupplier {
        private final String serviceId;

        public HelloServiceInstanceListSupplier(String serviceId) {
            this.serviceId = serviceId;
        }

        @Override
        public String getServiceId() {
            return this.serviceId;
        }

        @Override
        public Flux<List<ServiceInstance>> get() {
            return Flux.just(Arrays.asList(
                    new DefaultServiceInstance(serviceId + 1, serviceId, "localhost", 10001, false),
                    new DefaultServiceInstance(serviceId + 2, serviceId, "localhost", 10002, false)
            ));
        }
    }
}


